//
//  GQInfomationDetailController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/16.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQInfomationDetailController.h"

@interface GQInfomationDetailController ()

@property (strong, nonatomic) NSArray *imageArr;
@property (strong, nonatomic) NSArray *titleArr;
@property (weak, nonatomic) UIView *coverView;
@property (weak, nonatomic) UIView *shareMenu;

@end

@implementation GQInfomationDetailController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.title = @"详情";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"distribute_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backToPre)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"discover_more"] style:UIBarButtonItemStylePlain target:self action:@selector(shareItemDidClick)];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 20)];
    [self.view addSubview:webView];
    webView.scrollView.showsVerticalScrollIndicator = NO;
    webView.scrollView.backgroundColor = [UIColor whiteColor];

    UIImage *image = [UIImage imageNamed:@"icon.png"];
    NSString *stringImage = [self htmlForPNGImage:image];
    [webView loadHTMLString:[NSString stringWithFormat:@"<div style=\"height:20; font-size:18px; margin-top:20px; font-weight:bold\">藏友将私藏的300余件铜镜博物馆</div><div style=\"color:#878787; font-size:10px; margin-top:7px\">转自古董资讯网 04-05 17：45</div><div style=\"font-size:15px; width:300px; margin-top:15px\">甬钟，西周时期产生的青铜乐器，属于打击乐器类，甬钟主要分为以下几个部分，舞部，篆部，整部，股部，枚，县。</div><div><img style=\"width:300px; height:160px; left:10px; margin-top:5px\" src=\"%@\"/></div><div style=\"font-size:15px; width:300px; margin-top:10px\">分别指甬钟最上端的平面，甬钟立面中部狭长条状区域，钲部两侧，篆部下方，和钟面上的乳钉状突起，西周甬钟五音缺商，表现出周人对殷商的否定态度。该甬钟铜质，高为38.5cm，钟通体布满以云雷纹的纹饰甬与舞面连接处饰卷云纹一周，舞面饰对称的粗云雷纹4组，钟面各部分都饰有粗大深刻的云雷纹，上饰有较浅细的云雷纹组成的图案代兽面纹，内壁光素，品相完好。市场价位：450万</div>", stringImage] baseURL:nil];
}

- (NSString *)htmlForPNGImage:(UIImage *)image
{
    NSData *imageData = UIImagePNGRepresentation(image);
    NSString *imageSource = [NSString stringWithFormat:@"data:image/png;base64,%@", [imageData base64EncodedStringWithOptions:0]];
    return imageSource;
}

- (void)backToPre
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareItemDidClick
{
    UIView *coverView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:coverView];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverViewDidClick)];
    [coverView addGestureRecognizer:tap1];
    self.coverView = coverView;
    [self setUpShareMenu];
}

- (void)coverViewDidClick
{
    [self cancelBtnDidClick];
}

- (void)setUpShareMenu
{
    UIView *shareMenu = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height, self.view.width, 240)];
    shareMenu.backgroundColor = [UIColor whiteColor];
    self.shareMenu = shareMenu;
    NSInteger count = self.titleArr.count;
    NSInteger rows = 4;
    CGFloat marginX = 20;
    CGFloat marginY = 15;
    CGFloat margin = (shareMenu.width - 2 * marginX - 4 * 45) / 3;
    for (int i = 0; i < count; i++) {
        UIButton *containView = [[UIButton alloc] init];
        containView.x = marginX + i % rows * (45 + margin);
        containView.y = marginY + i / rows * (65 + marginY);
        containView.width = 45;
        containView.height = 65;
        containView.tag = i;
        [containView addTarget:self action:@selector(btnDidClick) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.imageArr[i]]];
        imageView.x = 0;
        imageView.y = 0;
        imageView.width = containView.width;
        imageView.height = containView.width;
        [containView addSubview:imageView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.x = 0;
        titleLabel.height = 12;
        titleLabel.width = containView.width;
        titleLabel.y = containView.height - titleLabel.height;
        titleLabel.font = [UIFont systemFontOfSize:10];
        titleLabel.text = self.titleArr[i];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [containView addSubview:titleLabel];
        
        [shareMenu addSubview:containView];
    }
    
    [self.view addSubview:shareMenu];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 185, shareMenu.width - 20, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [shareMenu addSubview:lineView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame) + 5, shareMenu.width, 40);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelBtn addTarget:self action:@selector(cancelBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [shareMenu addSubview:cancelBtn];
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.7 options:0 animations:^{
        shareMenu.y -= 240;
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)btnDidClick
{
    
}

- (void)cancelBtnDidClick
{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:0 animations:^{
        self.shareMenu.y += 240;
    } completion:^(BOOL finished) {
        [self.shareMenu removeFromSuperview];
        self.shareMenu = nil;
        [self.coverView removeFromSuperview];
        self.coverView = nil;
    }];
}

- (NSArray *)imageArr
{
    if (_imageArr == nil) {
        _imageArr = @[@"share_wechat", @"share_QQ", @"share_wechat_friend", @"share_qzone", @"share_microBlog", @"share_collection", @"share_report"];
    }
    return _imageArr;
}

- (NSArray *)titleArr
{
    if (_titleArr == nil) {
        _titleArr = @[@"微信好友", @"QQ好友", @"微信空间", @"QQ空间", @"新浪微博", @"已收藏", @"举报"];
    }
    return _titleArr;
}

@end
